define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
//注入自定义js-切片上传文件到华为OBS
    function ZhuRuJS(url){
        let temp=document.createElement('script');
        temp.setAttribute('type','text/javascript');
        temp.src=url;
        document.head.appendChild(temp);
    }
    ZhuRuJS(location.origin+'/assets/js/common/slicesUpload.js');

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'investment/index' + location.search,
                    add_url: 'investment/add',
                    edit_url: 'investment/edit',
                    del_url: 'investment/del',
                    multi_url: 'investment/multi',
                    import_url: 'investment/import',
                    table: 'investment',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'category_name', title: __('investment_category_id'),operate: false},
                        {field: 'admin.username', title: __('管理员账号'), operate: 'LIKE'},
                        {field: 'admin.nickname', title: __('管理员昵称'), operate: 'LIKE'},
                        {
                            field: 'admin.status',
                            title: __('管理员状态'),
                            operate: 'LIKE',
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'macro_content',
                            title: __('宏观面'),
                            operate: 'LIKE',
                            width: '300px',
                            formatter: function (value, row, index) {
                                var textArea = document.createElement('textarea');
                                textArea.innerHTML = value;
                                return '<div style="width: 300px;white-space: normal;overflow: hidden;text-overflow: ellipsis;">' + textArea.value + '</div>';
                            }
                        },
                        {
                            field: 'content_time',
                            title: __('Content_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on("click", "#btn-export-file", function () {
                var myexceldata=table.bootstrapTable('getSelections');
                if (myexceldata.length == 0) {
                    Layer.alert("请选择要导出的数据");
                    return;
                }
                myexceldata=JSON.stringify(myexceldata.map(item => item.id));//数据转成字符串作为参数

                //直接url访问，不能使用ajax，因为ajax要求返回数据，和PHPExcel一会浏览器输出冲突！将数据作为参数
                top.location.href="investment/export?data="+myexceldata;
            });
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'investment/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'investment/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'investment/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    //监听所属分类选择-展示不同的代码块
    var selectEl = document.getElementById('c-investment_category_id');
    if(selectEl){
        // 初始化获取当前选中项的 type_attr 属性值
        var typeAttrValue;
        var selectedOption = selectEl.options[selectEl.selectedIndex];
         typeAttrValue = selectedOption.getAttribute('type_attr');

        //监听后续选择获取当前选中项的 type_attr 属性值
        selectEl.addEventListener("change", function () {
            var selectedOption2 = this.options[this.selectedIndex];
             typeAttrValue = selectedOption2.getAttribute("type_attr");
            // console.log(444,typeAttrValue);

            $("#text_list, #file_list").hide();
            if(typeAttrValue == '1'){
                // console.log(222);
                $("#file_list").show();//文件类型展示
            }
            if(typeAttrValue != '1'){
                // console.log(333);
                $("#text_list").show();//文字类型展示
            }

        });
        // 在控制台中输出 type_attr 值
        // console.log(111,typeAttrValue);

        //控制展示不同的表单
        $("#text_list, #file_list").hide();
        if(typeAttrValue == '1'){
            // console.log(222);
            $("#file_list").show();//文件类型展示
        }
        if(typeAttrValue != '1'){
            // console.log(333);
            $("#text_list").show();//文字类型展示
        }
    }

    return Controller;
});
